package cn.springboot.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Role;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
@Api("角色管理")
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation("角色列表")
    @GetMapping("roleName")
    @RequiresPermissions("sys:role:select")
    public ResponseResult roleName(){
       List<Role> roleList =  roleService.list();
       return ResponseResult.Success().data("roleName",roleList);
    }

    @Log(description = Operation.SELECT,model = "角色管理")
    @ApiOperation("分页查询")
    @PostMapping("page")
    @RequiresPermissions("sys:role:select")
    public ResponseResult page(@RequestBody QueryParams<Role> queryParams){

        return roleService.rolePage(queryParams);
    }

    @Log(description = Operation.SELECT,model = "角色管理")
    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    @RequiresPermissions("sys:role:select")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        Role role = roleService.getById(id);
        return ResponseResult.Success().data("role",role);
    }

    @Log(description = Operation.ADDORUPDATE,model = "角色管理")
    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    @RequiresPermissions(value = {"sys:role:add","sys:role:update"})
    public ResponseResult insertOrUpdate(@RequestBody Role role){
        // 根据角色名称查询数据库
        // 获取角色名
        String roleName = role.getRoleName();
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getRoleName,roleName);
        Role roleOne = roleService.getOne(lambda);

        // 如果roleOne不为空，则做编辑操作
        if (ObjectUtils.isNotNull(roleOne)){
            // 如果id相同，则执行编辑操作
            if (ObjectUtil.equal(roleOne.getId(),role.getId())){
                roleService.saveOrUpdate(role);
                return ResponseResult.Success().msg("编辑成功");
            }
            // 否则返回提示信息
            return ResponseResult.Fail().msg("角色名已经存在");
        }
        // 如果roleOne为空，则执行添加操作
        roleService.saveOrUpdate(role);
        return ResponseResult.Success().msg("添加成功");
    }

    @Log(description = Operation.DELETE,model = "角色管理")
    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    @RequiresPermissions("sys:role:delete")
    public ResponseResult deleteById(@PathVariable("id") Integer id){

        return roleService.deleteById(id);
    }

    @Log(description = Operation.SELECT,model = "角色管理")
    @ApiOperation("角色id获取权限")
    @GetMapping("selectByIdPermission/{id}")
    @RequiresPermissions("sys:role:select")
    public ResponseResult selectByIdPermission(@PathVariable("id") Integer id){

        return roleService.selectByIdPermission(id);
    }

    @Log(description = Operation.UPDATE,model = "角色管理")
    @ApiOperation("分配权限")
    @PostMapping("updateRolePermission")
    @RequiresPermissions("sys:role:update")
    public ResponseResult updateRolePermission(@RequestBody Map<String,Object> map){
        return roleService.updateRolePermission(map);
    }

}
